package com.yxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxh.base.PageEntity;
import com.yxh.dao.KengMapper;
import com.yxh.dao.WorkMapper;
import com.yxh.dao1.NewWorkMapper;
import com.yxh.exception.UserException;
import com.yxh.exception.WordbookException;
import com.yxh.pojo.Keng;
import com.yxh.pojo.Wordbook;
import com.yxh.pojo.Work;
import com.yxh.pojo.vo.WorkVO;
import com.yxh.service.WorkService;
import com.yxh.util.APIErrorCode;
import com.yxh.util.SnowflakeIdWorker;
import com.yxh.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yxh.util.APIErrorCode.*;

/**
 * Created by 32988 on 2018/6/12.
 */
@Service("workService")
@Transactional
public class WorkServiceImpl implements WorkService {

    @Autowired
    WorkMapper workMapper;

    @Autowired
    private NewWorkMapper newWorkMapper;

    @Autowired
    private KengMapper kengMapper;

    @Override
    public PageInfo getInformation(Keng record, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Map<String,Object>> workList = kengMapper.selectByWorks(record);
        PageInfo pageInfo = new PageInfo(workList);
        return pageInfo;
    }

    @Override
    public PageInfo getInformation(String value, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Map<String,Object>> workList = kengMapper.selectByWorksApp(value);
        PageInfo pageInfo = new PageInfo(workList);
        return pageInfo;
    }


    @Override
    public void oacleToMysql() {
         int     j=0,oacleleng=newWorkMapper.selectCount(),pages = 0;
            pages=oacleleng%50==0?oacleleng/50:oacleleng/50+1;
            for(int i=0;i<pages;i++){
                Map<String,Object> map=new HashMap<String, Object>();
                map.put("index",i*50+1);
                map.put("last",(i+1)*50>oacleleng?oacleleng:(i+1)*50);
                List<Keng> test=newWorkMapper.selectByPrimaryKey(map);
                System.out.println(test);
                for(Keng k:test){
                    Keng kk= kengMapper.selectByPrimaryKey(k.getObjId());
                    if(kk==null)
                    j= kengMapper.insertSelective(k);
                    else
                    j=kengMapper.updateByPrimaryKeySelective(k);
                    if(j==0)
                        throw new UserException(APIErrorCode.SYNCHRO_ERROR);
                }
            }
    }

    @Override
    public int save(Keng record) {
       // record.setWorkId(SnowflakeIdWorker.creatId());
        int count = kengMapper.insertSelective(record);
        if (count == 0) {
            throw new WordbookException(INTERNAL_ERROR);
        }
        return count;
    }

    @Override
    public int update(Keng record) {
        int count = kengMapper.updateByPrimaryKeySelective(record);
        if (count == 0) {
            throw new WordbookException(PARAMS_INVALID);
        }
        return count;
    }

    @Override
    public int delete(String id) {

        if(!StringUtil.isEmpty(id)){
            String [] arr = id.split(",");
            int count = kengMapper.deleteByIds(arr);
            if (count == 0) {
                throw new WordbookException(PARAMS_INVALID);
            }
        }
        return 0;
    }

    @Override
    public void batchDelete(List<Keng> records) {

    }

    @Override
    public Keng get(String id) {
        return null;
    }

    @Override
    public Keng get(Keng record) {
        return null;
    }

    @Override
    public List<Keng> getList(Keng record) {
        return null;
    }

    @Override
    public List<Keng> getAllList(Keng record) {
        return null;
    }

    @Override
    public List<Keng> getListPage(PageEntity<Keng> record) {
        return null;
    }



}
